//
//  ListMovieViewController.m
//  HdMovie
//
//  Created by Classic on 12/14/12.
//  Copyright (c) 2012 Hoang tuan. All rights reserved.
//

#import "ListMovieViewController.h"
#import "CustomCellListMovie.h"
#import "Movie.h"
#import "CustomCellListMenu.h"
#import "DetailViewController.h"
#import "UpgradeAccountViewController.h"
#import "InfoAccViewController.h"
#import "MoviePlayViewController.h"

@interface ListMovieViewController ()

@end
NSArray *arrAcc;
NSArray *arrAccIcon;
NSArray *arrCat;
NSArray *arrCatIcon;
NSArray *arrYear;
NSArray *arrYearIcon;
NSArray *arrCountry;
NSArray *arrCountryIcon;
NSArray *arrListMenuItem;
NSArray *arrListMenuIcon;
NSArray *arrStar;

int indexListMenu = 0;
BOOL menuIconIsShow = NO;
BOOL menuListIsShow = NO;
BOOL settingIsShow = NO;
BOOL animEnded = NO;
@implementation ListMovieViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"viewDidLoad");
    // Do any additional setup after loading the view from its nib.
    UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)] autorelease];
    _txtFldSearch.leftView = paddingView;
    _txtFldSearch.leftViewMode = UITextFieldViewModeAlways;
    _txtFldSearch.delegate = self;
    
    mulArr = [[NSMutableArray alloc] init];
    NSArray *arrTitle = [[NSArray alloc] initWithObjects:@"Rambo",@"Kungfu",@"thanh long",@"chau tan", @"trieu vy", nil];
    NSArray * arrThumb = [[NSArray alloc] initWithObjects:@"rambo.jpeg",@"kungfu.jpeg",@"thanh-long.jpeg",@"chau-tan.jpeg",@"trieu-vy.jpeg", nil];
    arrStar = [[NSArray alloc] initWithObjects:@"1-star.png",@"2-star.png",@"3-star.png",@"4-star.png",@"5-star.png",@"6-star.png",@"7-star.png",@"8-star.png",@"9-star.png",@"10-star.png", nil];
    
    arrAcc = [[NSArray alloc] initWithObjects:@"Đăng xuất", @"Thông tin tài khoản",@"Nâng cấp tài khoản",nil];
    arrAccIcon = [[NSArray alloc] initWithObjects:@"ic-logout.png", @"ic-change-pass.png", @"ic-info-acc.png", @"ic-upgrade.png", nil];
    
    arrCat = [[NSArray alloc] initWithObjects:@"Hot nhất", @"Mới nhất", @"Chiếu rạp", @"Hành động", @"Phiêu lưu", @"Kinh dị", @"Tình cảm",@"Hoạt hình",@"Võ thuật", @"Hài hước",@"Tâm lý",@"Viễn tưởng",@"Thần thoại",@"Chiến tranh",@"Cổ trang",@"Âm nhạc",@"Hình sự",@"Tivi show",@"Phim 3D",@"Khoa học",@"Tài liệu",@"Lịch sử",@"Gia đình",@"Thể thao",@"Kiếm hiệp",@"Phim 18+",nil];
    arrCatIcon = [[NSArray alloc] initWithObjects:@"ic-hot-movie.png", @"ic-new-movie.png", @"ic-cinema-movie.png", @"ic-action-movie.png",@"ic-phieuluu-movie.png",@"ic-kinhdi-movie.png",@"ic-love-movie.png", @"ic-anim-movie.png",@"ic-vothuat-movie.png", @"ic-funny-movie.png",@"ic-tamly-movie.png",@"ic-vientuong-movie.png",@"ic-thanthoai-movie.png",@"ic-chientranh-movie.png",@"ic-cotrang-movie.png",@"ic-amnhac-movie.png",@"ic-hinhsu-movie.png",@"ic-tivishow-movie.png",@"ic-3d-movie.png",@"ic-khoahoc-movie.png",@"ic-tailieu-movie.png",@"ic-lichsu-movie.png",@"ic-giadinh-movie.png",@"ic-thethao-movie.png",@"ic-kiemhiep-movie.png",@"ic-18-movie.png", nil];
    
    arrYear = [[NSArray alloc] initWithObjects:@"2013",@"2012",@"2011",@"2010",@"2009",@"2008",@"2007",@"2006",@"2005",@"2004",@"2003",@"2002",@"2001",@"2000",@"1999",@"1998",@"1997",@"1996",@"1995",@"1994",@"1993",@"1992",@"1991",@"1990", nil];
    arrYearIcon = [[NSArray alloc] initWithObjects:@"ic-2013.png",@"ic-2012.png",@"ic-2011.png",@"ic-2010.png",@"ic-2009.png",@"ic-2008.png",@"ic-2007.png",@"ic-2006.png",@"ic-2005.png",@"ic-2004.png",@"ic-2003.png",@"ic-2002.png",@"ic-2013.png",@"ic-2012.png",@"ic-2009.png",@"ic-2008.png",@"ic-2007.png",@"ic-2006.png",@"ic-2005.png",@"ic-2004.png",@"ic-2003.png",@"ic-2002.png",@"ic-2013.png",@"ic-2012.png", nil];
    
    arrCountry = [[NSArray alloc] initWithObjects:@"Ai Len",@"Ấn Độ", @"Anh",@"Canada",@"Châu Á",@"Châu Âu",@"Đài Loan",@"Đức", @"Hàn Quốc",@"Hồng Kông",@"Hungary", @"Mỹ", @"Nga", @"Nhật Bản", @"Pháp",@"Tây Ban Nha",@"Thái Lan", @"Trung Quốc", @"Việt Nam", nil];
    arrCountryIcon = [[NSArray alloc] initWithObjects:@"ic-ailen-movie.png",@"ic-ando-movie.png",@"ic-anh-movie.png",@"ic-canada-movie.png",@"ic-chaua-movie.png",@"ic-chauau-movie.png",@"ic-dailoan-movie.png",@"ic-duc-movie.png",@"ic-hanquoc-movie.png",@"ic-hongkong-movie.png",@"ic-hungary-movie.png",@"ic-mi-movie.png",@"ic-nga-movie.png",@"ic-nhatban-movie.png",@"ic-phap-movie.png",@"ic-taybannha-movie.png",@"ic-thailan-movie.png",@"ic-trungquoc-movie.png", @"ic-vietnam-movie.png", nil];
    
    int year[] = {2012,2011,2012,2010,2000};
    int rate[] = {1,2,5,7,10};
    int like[] = {1000, 3000, 2000,500,1000};
    int n = arrTitle.count;
    for (int i = 0; i<n; i++) {
        Movie *movie = [[Movie alloc] init];
        movie.imgName = [arrThumb objectAtIndex:i];
        movie.title = [arrTitle objectAtIndex:i];
        movie.year = year[i];
        movie.rate = rate[i];
        movie.like = like[i];
        [mulArr addObject:movie];
    }
    
    arrListMenuItem = arrAcc;
    arrListMenuIcon = arrAccIcon;
    
    //set highlight cho button
    [_btnCategory setBackgroundImage:[UIImage imageNamed:NSLocalizedString(@"btn-category-select", @"btn cate")] forState:UIControlStateHighlighted];
    [_btnCategory setImage:[UIImage imageNamed:NSLocalizedString(@"btn-category-select", @"btn category select")] forState:UIControlStateDisabled];
    
    [_btnAcc setBackgroundImage:[UIImage imageNamed:NSLocalizedString(@"btn-acc-select", @"btn acc")] forState:UIControlStateHighlighted];
    [_btnAcc setImage:[UIImage imageNamed:NSLocalizedString(@"btn-acc-select", @"btn acc select")] forState:UIControlStateDisabled];
    
    [_btnSetting setBackgroundImage:[UIImage imageNamed:NSLocalizedString(@"btn-setting-select", @"btn setting")] forState:UIControlStateHighlighted];
    [_btnSetting setImage:[UIImage imageNamed:NSLocalizedString(@"btn-setting-select", @"btn setting select")] forState:UIControlStateDisabled];
    
    [_btnCountry setBackgroundImage:[UIImage imageNamed:NSLocalizedString(@"btn-country-select", @"btn country")] forState:UIControlStateHighlighted];
    [_btnCountry setImage:[UIImage imageNamed:NSLocalizedString(@"btn-country-select", @"btn country select")] forState:UIControlStateDisabled];
    
    [_btnYear setBackgroundImage:[UIImage imageNamed:NSLocalizedString(@"btn-year-select", @"btn year")] forState:UIControlStateHighlighted];
    [_btnYear setImage:[UIImage imageNamed:NSLocalizedString(@"btn-year-select", @"btn year select")] forState:UIControlStateDisabled];
    
    [self menuIconHide];
    [self listMenuHide];
    [self settingHide];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        return 1;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%d", tableView.tag);
    if (tableView.tag == 2){
        return  arrListMenuItem.count;
           }
    return mulArr.count;
        
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
        return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 2) {
        CustomCellListMenu *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellListMenu"];
                [cell.lblTitle setText:[arrListMenuItem objectAtIndex:indexPath.row]];
                [cell.imgIconMenu setImage:[UIImage imageNamed:[arrListMenuIcon objectAtIndex:indexPath.row]]];
                return cell;
    }
    
    CustomCellListMovie *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellListMovie"];
    Movie *movie = [mulArr objectAtIndex:indexPath.row];
    [cell.imgThumb setImage:[UIImage imageNamed:movie.imgName]];
    [cell.lblTitle setText:movie.title];
    [cell.lblViewCount setText:[NSString stringWithFormat:@"%d",movie.like]];
    [cell.lblTime setText:[NSString stringWithFormat:@"%d",movie.year]];
    [cell.lblCate setText:[NSString stringWithFormat:@"%d",movie.rate]];
    cell.btnPlay.movie = movie;
    [cell.btnPlay addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.imgRate setImage:[UIImage imageNamed:[arrStar objectAtIndex:movie.rate-1]]];
    return cell;
}
-(void)viewWillDisappear:(BOOL)animated{
    [_txtFldSearch resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}
- (void)dealloc {
    [self setTableMovie:nil];
    [self setTableMenu:nil];
    [self setViewIconMenu:nil];
    [self setBtnAcc:nil];
    [self setBtnSetting:nil];
    [self setBtnCategory:nil];
    [self setBtnYear:nil];
    [self setViewSetting:nil];
    [super dealloc];
}
//bat su kien button search
- (IBAction)searchClick:(id)sender {
    [self viewWillDisappear:YES];
}
//bat su kien vuot sang trai hoac sang phai
- (IBAction)swipe:(id)sender {
    if (menuIconIsShow) {
        [self menuIconHide];
        [self listMenuHide];
        [self settingHide];
    }else{
        [self menuIconShow];
    }
}
//hien thi menu ben phai
-(void)menuIconShow
{
    CGRect rootViewFrame = self.view.bounds; //lay kich thuoc man hinh
    //animation
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [_viewIconMenu setFrame:CGRectMake(rootViewFrame.size.width - 75, 0, 75, rootViewFrame.size.height)];
    [UIView commitAnimations];
    menuIconIsShow = YES;
}
//an menu ben phai
-(void)menuIconHide
{
    CGRect rootViewFrame = self.view.bounds; //lay kich thuoc man hinh
    //animation
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [_viewIconMenu setFrame:CGRectMake(rootViewFrame.size.width, 0, 75, rootViewFrame.size.height)];
    [UIView commitAnimations];
    menuIconIsShow = NO;
    [_btnAcc setEnabled:TRUE];
    [_btnCategory setEnabled:TRUE];
    [_btnYear setEnabled:TRUE];
    [_btnCountry setEnabled:TRUE];
    [_btnSetting setEnabled:TRUE];
}
//hien thi menu ben trai
-(void)listMenuShow
{
    CGRect rootViewFrame = self.view.bounds; //lay kich thuoc man hinh
    //animation
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [_tableMenu setFrame:CGRectMake(0, 0, 245, rootViewFrame.size.height)];
    animEnded = YES;
    [UIView commitAnimations];
    menuListIsShow = YES;
}
//an menu ben trai
-(void)listMenuHide
{
    CGRect rootViewFrame = self.view.bounds; //lay kich thuoc man hinh
    //animation
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [_tableMenu setFrame:CGRectMake(-245, 0, 245, rootViewFrame.size.height)];
    animEnded = YES;
    [UIView commitAnimations];
    menuListIsShow = NO;
}
//hien thi view setting
-(void)settingShow
{
    CGRect rootViewFrame = self.view.bounds; //lay kich thuoc man hinh
    //animation
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [_viewSetting setFrame:CGRectMake(0, 0, 245, rootViewFrame.size.height)];
    animEnded = YES;
    [UIView commitAnimations];
    settingIsShow = YES;
}
//an view setting
-(void)settingHide
{
    CGRect rootViewFrame = self.view.bounds; //lay kich thuoc man hinh
    //animation
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [_viewSetting setFrame:CGRectMake(-245, 0, 245, rootViewFrame.size.height)];
    animEnded = YES;
    [UIView commitAnimations];
    settingIsShow = NO;
}
//bat su kien button play
- (IBAction)playClick:(MovieButton *)sender{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    MoviePlayViewController *moviePlay = [storyboard instantiateViewControllerWithIdentifier:@"MoviePlayViewController"];
    moviePlay.movie = sender.movie;
    [moviePlay setModalPresentationStyle:UIModalPresentationFullScreen];
    moviePlay.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:moviePlay animated:YES];
}
//bat su kien khi bam chon 1 row
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //tag = 2 la menu ben trai
    if (tableView.tag == 2) {
        //click item from menu
        switch (indexListMenu) {
            case 1:
                switch (indexPath.row) {
                    case 0:
                        [self dismissModalViewControllerAnimated:YES];
                        break;
                    case 1:
                        [self infoAccDisplay];
                        break;
                    case 2:
                        [self upgradeAccDisplay];
                        break;
                    default:
                        break;
                }

                break;
            case 2:
                //list category
                break;
            case 3:
                //list year
                break;
            case 4:
                //list country
                break;
            default:
                break;
        }
        [self menuIconHide];
        [self listMenuHide];
        [self settingHide];
    }else{
        //click item from list movie
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        DetailViewController *detailMovie = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
        Movie *movie = [mulArr objectAtIndex:indexPath.row];
        //gui thong tin phim sang man hinh detail
        detailMovie.movie = movie;
        [detailMovie setModalPresentationStyle:UIModalPresentationFullScreen];
        detailMovie.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:detailMovie animated:YES];
    }
} 

//hien thi man hinh thong tin tai khoan
-(void)infoAccDisplay
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    InfoAccViewController *info = [storyboard instantiateViewControllerWithIdentifier:@"InfoAccViewController"];
    [info setModalPresentationStyle:UIModalPresentationFullScreen];
    info.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:info animated:YES];
}
//hien thi man hinh nang cap tai khoan
-(void)upgradeAccDisplay
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UpgradeAccountViewController *upgrade = [storyboard instantiateViewControllerWithIdentifier:@"UpgradeAccountViewController"];

    [upgrade setModalPresentationStyle:UIModalPresentationFullScreen];
    upgrade.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:upgrade animated:YES];

}

- (void)viewDidUnload {
    [self setTableMovie:nil];
    [self setTableMenu:nil];
    [self setViewIconMenu:nil];
    [self setBtnAcc:nil];
    [self setBtnSetting:nil];
    [self setBtnCategory:nil];
    [self setBtnYear:nil];
    [self setViewSetting:nil];
    [super viewDidUnload];
}
//bat su kien button tai khoan/account
- (IBAction)btnAccClick:(id)sender {
    NSLog(@"btn acc");
    if (indexListMenu != 1) {
        indexListMenu = 1;
    //cap nhat lai tableView menu ben trai
    arrListMenuItem = arrAcc;
    arrListMenuIcon = arrAccIcon;
    [_tableMenu reloadData];
    if (menuListIsShow) {
        [self listMenuHide];
    }
    }
    if (settingIsShow) {
        [self settingHide];
    }
    //cho phep hoac ko cho phep bam button
    [_btnAcc setEnabled:FALSE];
    [_btnCategory setEnabled:TRUE];
    [_btnYear setEnabled:TRUE];
    [_btnCountry setEnabled:TRUE];
    [_btnSetting setEnabled:TRUE];
    //cho 1 khoang thoi gian la 0.2 giay roi thuc hien ham listMenuShow
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(listMenuShow) userInfo:nil repeats:NO];
}
//bat su kien button country
- (IBAction)btnCountryClick:(id)sender {
    NSLog(@"btn country");
    if (indexListMenu != 4) {
    indexListMenu = 4;
    //cap nhat lai tableView menu ben trai
    arrListMenuItem = arrCountry;
    arrListMenuIcon = arrCountryIcon;
    [_tableMenu reloadData];
        
    if (menuListIsShow) {
        [self listMenuHide];
    }}
    if (settingIsShow) {
        [self settingHide];
    }
    //cho phep hoac ko cho phep bam button
    [_btnAcc setEnabled:TRUE];
    [_btnCategory setEnabled:TRUE];
    [_btnYear setEnabled:TRUE];
    [_btnCountry setEnabled:FALSE];
    [_btnSetting setEnabled:TRUE];
    //cho 1 khoang thoi gian la 0.2 giay roi thuc hien ham listMenuShow
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(listMenuShow) userInfo:nil repeats:NO];

}
//bat su kien button setting
- (IBAction)btnSettingClick:(id)sender {
    NSLog(@"btn setting");
    if (menuListIsShow) {
        [self listMenuHide];
    }
    //cho phep hoac ko cho phep bam button
    [_btnAcc setEnabled:TRUE];
    [_btnCategory setEnabled:TRUE];
    [_btnYear setEnabled:TRUE];
    [_btnCountry setEnabled:TRUE];
    [_btnSetting setEnabled:FALSE];
    //cho 1 khoang thoi gian la 0.2 giay roi thuc hien ham settingShow
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(settingShow) userInfo:nil repeats:NO];
 }
//bat su kien button year
- (IBAction)btnYearClick:(id)sender {
    NSLog(@"btn year");
    if (indexListMenu != 3) {
        indexListMenu = 3;
        //cap nhat lai tableView menu ben trai
        arrListMenuItem = arrYear;
        arrListMenuIcon = arrYearIcon;
        [_tableMenu reloadData];
    if (menuListIsShow) {
        [self listMenuHide];
    }}
    if (settingIsShow) {
        [self settingHide];
    }
    //cho phep hoac ko cho phep bam button
    [_btnAcc setEnabled:TRUE];
    [_btnCategory setEnabled:TRUE];
    [_btnYear setEnabled:FALSE];
    [_btnCountry setEnabled:TRUE];
    [_btnSetting setEnabled:TRUE];
    //cho 1 khoang thoi gian la 0.2 giay roi thuc hien ham listMenuShow
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(listMenuShow) userInfo:nil repeats:NO];
}
//bat su kien button category
- (IBAction)btnCategoryClick:(id)sender {
    NSLog(@"btn cate");
    if (indexListMenu != 2) {
        indexListMenu = 2;
        arrListMenuItem = arrCat;
        arrListMenuIcon = arrCatIcon;
        [_tableMenu reloadData];
    if (menuListIsShow) {
        [self listMenuHide];
    }}
    if (settingIsShow) {
        [self settingHide];
    }
    [_btnAcc setEnabled:TRUE];
    [_btnCategory setEnabled:FALSE];
    [_btnYear setEnabled:TRUE];
    [_btnCountry setEnabled:TRUE];
    [_btnSetting setEnabled:TRUE];
    //cho 1 khoang thoi gian la 0.2 giay roi thuc hien ham listMenuShow
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(listMenuShow) userInfo:nil repeats:NO];
}
@end
